package com.ruoyi.backend.controller;

import com.ruoyi.backend.pojo.Survey;
import com.ruoyi.backend.pojo.SurveyData;
import com.ruoyi.backend.pojo.SurveyListData;
import com.ruoyi.backend.service.SurveyService;
import com.ruoyi.backend.vo.FieldSurveyDataVO;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.DataSourceType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "调查数据表管理相关接口", value = "MySQL数据库主导执行(所有接口均已通过测试)")
@RequestMapping("/backend/survey")
@RestController
public class SurveyController {

    @Autowired
    private SurveyService surveyService;

    /**
     * 对于接口文档中特殊的数据结构需要在接口访问层进行微调以将list中数据循环遍历出来插入
     * @param survey
     * @return
     */
    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "上传调查数据表接口")
    @PostMapping("/upload")
    public R<String> upload(@RequestBody @Validated(Survey.Upload.class) Survey survey) {

        Integer investigateId = survey.getInvestigateId();
        for (SurveyListData surveyData : survey.getSurveyDataList()) {
            surveyService.upload(surveyData, investigateId);
        }
        return R.ok("调查数据表上传成功");
    }

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "查询调查数据表接口")
    @GetMapping("/search")
    public R<List<FieldSurveyDataVO>> search(@RequestParam(name = "investigateId", required = false) Integer investigateId,
                                             @RequestParam(name = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime startTime,
                                             @RequestParam(name = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime endTime,
                                             @RequestParam(name = "longitude", required = false) Double longitude,
                                             @RequestParam(name = "latitude", required = false) Double latitude,
                                             @RequestParam(name = "height", required = false) Double height
                         ) {

        List<FieldSurveyDataVO> surveyDataList = surveyService.search(investigateId, startTime, endTime, longitude, latitude, height);
        return R.ok(surveyDataList, "调查数据表查询成功");
    }

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "删除调查数据表接口")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam("id") @NotNull(message = "调查数据ID不能为空") Integer Id) {

        surveyService.delete(Id);
        return R.ok("调查数据表删除成功");
    }

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "更新调查数据表接口")
    @PutMapping("/update")
    public R<String> update(@RequestBody @Validated(SurveyData.Update.class) SurveyData survey) {

        surveyService.update(survey);
        return R.ok("调查数据表更新成功");
    }

}
